<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 段位控制器
 * @package app\index\controller
 */
class Duanwei extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取门票列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $page = $this->request->post('page')? '' : 1;
            $limit = $this->request->post('page')? '' : 10;
            $data = Db::name('qt_duanwei')
                ->field('a.*,u.real_name,p.name as project_name,g.name as product_name,g.price,u.work_number')
                ->alias('a')
                ->leftJoin('qt_users u','u.id = a.user_id')
                ->leftJoin('qt_project p','p.id = a.project_id')
                ->leftJoin('qt_goods g','g.id = a.product_id')
                ->where(['a.store_id' => $userInfo['store_id']])
                ->order('a.id desc')
                ->paginate($limit)->each(function ($item){
                    $typeIds = explode(',',$item['engineer_id']);
                    foreach ($typeIds as $key=>$value){
                        $engineer[$key] = Db::name('qt_users')->where('id',$value)->value('work_number');
                    }
                    $item['engineer_id'] = implode(',',$engineer);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 添加段位
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['project_id']) || empty($getData['product_id']) || empty($getData['engineer_id'])){
                $this->result('缺少参数', '', 204);
            }
            $engineerIds = explode(',',$getData['engineer_id']);
            foreach ($engineerIds as $key=>$value){
                $find = Db::name('qt_duanwei')
                    ->where(['store_id'=>$this->userInfo['store_id'],'project_id'=>$getData['project_id']])
                    ->whereLike('engineer_id','%'.$value.'%')
                    ->find();
                if(!empty($find)){
                    $engineerWorkNumber = Db::name('qt_users')->where('id',$value)->value('work_number');
                    $projectName = Db::name('qt_project')->where('id',$getData['project_id'])->value('name');
                    $this->result('['.$engineerWorkNumber.']技师项目['.$projectName.']段位费已存在', '', 204);
                }
            }
            //商品无法添加限制
            $find = Db::name('qt_goods')->whereIn('id',$getData['product_id'])->where(['status'=>'0'])->find();
            if(!empty($find)){
                $this->result('【'.$find['name'].'】不可售，无法添加段位费', '', 204);
            }
            $insert = [
                'project_id'=>$getData['project_id'],
                'product_id'=>$getData['product_id'],
                'engineer_id'=>$getData['engineer_id'],
                'user_id'=>$this->userInfo['id'],
                'store_id'=>$this->userInfo['store_id'],
                'create_time'=>time(),
            ];
            $insert = Db::name('qt_duanwei')->insert($insert);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑段位
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['project_id']) || empty($getData['product_id']) || empty($getData['engineer_id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_duanwei')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑段位
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_duanwei')
                ->field('a.*,u.real_name,p.name as project_name,g.name as product_name,g.price')
                ->alias('a')
                ->leftJoin('qt_users u','u.id = a.user_id')
                ->leftJoin('qt_project p','p.id = a.project_id')
                ->leftJoin('qt_goods g','g.id = a.product_id')
                ->where('a.id',$getData['id'])
                ->find();
            $typeIds = explode(',',$data['engineer_id']);
            foreach ($typeIds as $key=>$value){
                $engineer[$key] = Db::name('qt_users')->where('id',$value)->value('real_name');
                $typeIds[$key] = intval($value);
            }
            $data['engineer_name'] = $engineer;
            $data['engineer_id'] = $typeIds;
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除门票
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_duanwei')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
